package com.zxl.admin.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.zxl.common.constant.bookorder.BookOrderAuditStatusConstant;
import com.zxl.common.constant.CommonStatusConstant;
import com.zxl.common.exception.BusinessException;
import com.zxl.common.mapper.SysBookOrderMapper;
import com.zxl.common.mapper.SysMemberMapper;
import com.zxl.common.model.bo.order.BookOrderApply;
import com.zxl.common.model.bo.order.BookOrderAuditStatus;
import com.zxl.common.model.pojo.SysBookOrder;
import com.zxl.common.model.pojo.SysMember;
import com.zxl.common.model.query.order.BookOrderQuery;
import com.zxl.common.model.vo.member.MemberInfoDetailVo;
import com.zxl.common.model.vo.order.BookOrderVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zxw
 * @date 2023/4/1
 */
@Service
public class BookOrderService {
    @Resource
    private SysBookOrderMapper sysBookOrderMapper;
    @Resource
    private SysMemberMapper sysMemberMapper;

    public List<BookOrderVo> getBookOrderList(BookOrderQuery bookOrderQuery) {
        PageHelper.startPage(bookOrderQuery.getPageNumber(), bookOrderQuery.getPageSize());
        List<BookOrderVo> bookOrderList = sysBookOrderMapper.getBookOrderList(bookOrderQuery);
        return bookOrderList;
    }


    @Transactional(rollbackFor = Exception.class)
    public void updateBookAuditStatus(BookOrderAuditStatus bookOrderAuditStatus) {
        SysBookOrder sysBookOrder = new SysBookOrder();
        sysBookOrder.setId(bookOrderAuditStatus.getId());
        sysBookOrder.setAuditStatus(bookOrderAuditStatus.getAuditStatus());
        sysBookOrderMapper.updateById(sysBookOrder);

        SysMember sysMember = new SysMember();
        sysMember.setId(bookOrderAuditStatus.getMemberId());
        if (bookOrderAuditStatus.getAuditStatus().equals(BookOrderAuditStatusConstant.AUDIT_STATUS_ADOPT.getCode())){
            sysMember.setBookStatus(CommonStatusConstant.ADOPT.getCode());
        }else {
            sysMember.setBookStatus(CommonStatusConstant.PROHIBIT.getCode());
        }
        sysMemberMapper.updateById(sysMember);
    }

    public List<MemberInfoDetailVo> queryMember(String nickName) {
        List<MemberInfoDetailVo> memberInfoDetailVos = sysMemberMapper.selectMemberByName(nickName);
        return memberInfoDetailVos;
    }

    @Transactional(rollbackFor = Exception.class)
    public void addBookOrder(BookOrderApply bookOrderApply) {
        SysBookOrder sysBookOrder = sysBookOrderMapper.selectOne(new QueryWrapper<SysBookOrder>()
                .eq("member_id", bookOrderApply.getMemberId()));
        if (sysBookOrder == null) {
            SysBookOrder bookOrder = new SysBookOrder();
            BeanUtil.copyProperties(bookOrderApply, bookOrder);
            bookOrder.setAuditStatus(BookOrderAuditStatusConstant.AUDIT_STATUS_ADOPT.getCode());
            sysBookOrderMapper.insert(bookOrder);
        }else {
            throw new BusinessException("认证店铺记录已存在");
        }
        SysMember sysMember = sysMemberMapper.selectOne(new QueryWrapper<SysMember>()
                .eq("id", bookOrderApply.getMemberId()));
        BeanUtil.copyProperties(bookOrderApply,sysMember);
        sysMember.setBookStatus(BookOrderAuditStatusConstant.AUDIT_STATUS_ADOPT.getCode());
        sysMemberMapper.updateById(sysMember);
    }

    public void updateBookOrder(BookOrderApply bookOrderApply) {
        SysBookOrder sysBookOrder = sysBookOrderMapper.selectOne(new QueryWrapper<SysBookOrder>()
                .eq("member_id", bookOrderApply.getMemberId()));
        BeanUtil.copyProperties(bookOrderApply, sysBookOrder);
        sysBookOrderMapper.updateById(sysBookOrder);
        SysMember sysMember = sysMemberMapper.selectOne(new QueryWrapper<SysMember>()
                .eq("id", bookOrderApply.getMemberId()));
        BeanUtil.copyProperties(bookOrderApply,sysMember);
        sysMemberMapper.updateById(sysMember);
    }
}
